package com.hkyp.ss.course.provider.impl;

import com.hkyp.ss.course.apis.CourCommVoterService;
import com.hkyp.ss.course.apis.dto.response.CourCommVoterVO;
import com.hkyp.ss.course.apis.enumu.ActionTypeEnum;
import com.hkyp.ss.course.apis.exception.DaoException;
import com.hkyp.ss.course.provider.model.CourseComment;
import com.hkyp.ss.course.provider.model.mediate.CourCommVoter;
import com.hkyp.ss.course.provider.repository.CourCommRepository;
import com.hkyp.ss.course.provider.repository.UserRepository;
import com.hkyp.ss.course.provider.repository.mediate.CourCommVoterRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Slf4j
@Service(version = "1.0")
@Transactional(rollbackFor = Exception.class)
public class CourCommVoterServiceImpl implements CourCommVoterService {

    @Autowired
    CourCommVoterRepository courCommVoterRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    CourCommRepository courCommRepository;



    @Override
    public CourCommVoterVO doCourseCommentVoting(String type, Long CCId, Long userId) {
        ActionTypeEnum actionTypeEnum = ActionTypeEnum.valueOf(type.toUpperCase());
        Integer typeCode = actionTypeEnum.getTypeCode();

        //从数据库中查出该记录，有则更新，无则新建
        CourCommVoter courCommVoterFromDB = courCommVoterRepository.findByUserIdAndCcId(userId, CCId);

        CourseComment courseComment =courCommRepository.findById(CCId)
                .orElseThrow(()->new DaoException("此课程不存在"));
        Integer like = courseComment.getUserLike();
        Integer dikLike = courseComment.getUserDislike();

        CourCommVoterVO courCommVoterVO = new CourCommVoterVO();
        courCommVoterVO.setAction(actionTypeEnum.getTypeCode());

        //如果用户没有和此文章有过任何操作
        if(courCommVoterFromDB == null){
            CourCommVoter courCommVoter = new CourCommVoter();
            courCommVoter.setCcId(CCId);
            courCommVoter.setUserId(userId);
            courCommVoter.setCreateAt(System.currentTimeMillis());
            courCommVoter.setType(typeCode);
            courCommVoterRepository.save(courCommVoter);

            switch (actionTypeEnum) {
                case UP:
                    courseComment.setUserLike(like+1);
                    break;
                case NEUTRAL:
                    log.warn("一个没有点过赞的用户发起了中立操作，罕见");
                    break;
                case DOWN:
                    courseComment.setUserDislike(dikLike+1);
                    break;
            }
            courCommRepository.save(courseComment);
            courCommVoterVO.setDislike(courseComment.getUserDislike());
            courCommVoterVO.setLike(courseComment.getUserLike());

            return courCommVoterVO;

        }

        Integer originType = courCommVoterFromDB.getType();

        switch (actionTypeEnum) {
            case UP:
                courCommVoterFromDB.setType(typeCode);
                courCommVoterRepository.save(courCommVoterFromDB);
                if(Objects.equals(originType,ActionTypeEnum.DOWN.getTypeCode())){
                    //如果表中的actionType是踩，此时点赞需要有两个改变: like+1 dislike-1
                    courseComment.setUserLike(like+1);
                    courseComment.setUserDislike(dikLike-1);
                }else if(Objects.equals(originType,ActionTypeEnum.NEUTRAL.getTypeCode())){
                    //如果表中的actionType是中立，此时点赞需要有一个改变: like+1
                    courseComment.setUserLike(like+1);
                }else if(Objects.equals(originType,ActionTypeEnum.UP.getTypeCode())){
                    //如果表中的actionType是赞，说明这个赞是个并发操作

                    //此时不需要做任何改变
                }
                break;
            case NEUTRAL:
                //中立操作将此记录删除
                courCommVoterRepository.deleteById(courCommVoterFromDB.getId());

                if(Objects.equals(originType,ActionTypeEnum.DOWN.getTypeCode())){
                    //如果表中的originType是踩  dislike-1
                    courseComment.setUserDislike(dikLike-1);
                }else if(Objects.equals(originType,ActionTypeEnum.NEUTRAL.getTypeCode())){
                    //并发操作
                }else if(Objects.equals(originType,ActionTypeEnum.UP.getTypeCode())){
                    //如果表中的originType是赞
                    courseComment.setUserLike(like-1);
                }
                break;
            case DOWN:
                courCommVoterFromDB.setType(typeCode);
                courCommVoterRepository.save(courCommVoterFromDB);
                if(Objects.equals(originType,ActionTypeEnum.DOWN.getTypeCode())){
                    //并发操作
                }else if(Objects.equals(originType,ActionTypeEnum.NEUTRAL.getTypeCode())){
                    //如果表中的originType是中立  点击踩 dislike+1
                    courseComment.setUserDislike(dikLike+1);
                }else if(Objects.equals(originType,ActionTypeEnum.UP.getTypeCode())){
                    //如果表中的originType是赞 dislike+1 like-1
                    courseComment.setUserLike(like-1);
                    courseComment.setUserDislike(dikLike+1);
                }
                break;
        }

        courCommRepository.save(courseComment);
        courCommVoterVO.setDislike(courseComment.getUserDislike());
        courCommVoterVO.setLike(courseComment.getUserLike());

        return courCommVoterVO;

    }


    //测试成功
    public static void main(String[] args) {
        String type = "up";
        ActionTypeEnum actionTypeEnum = ActionTypeEnum.valueOf(type.toUpperCase());
        System.out.println(actionTypeEnum.getTypeCode());
    }
}
